{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 1.8 综合应用示例"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.多项式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.020894Z",
     "end_time": "2024-05-07T21:11:14.158440Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "poly1d([ 1, -2,  1])"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1, -2, 1])\n",
    "p = np.poly1d(a)\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "numpy.poly1d"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(p)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.158440Z",
     "end_time": "2024-05-07T21:11:14.202227Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "0"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p(1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.174502Z",
     "end_time": "2024-05-07T21:11:14.202227Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "-1.0"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = np.poly1d([1, 3, 1])\n",
    "f = lambda x: g(x - 1)\n",
    "f(0.0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.202227Z",
     "end_time": "2024-05-07T21:11:14.218047Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.218047Z",
     "end_time": "2024-05-07T21:11:14.285915Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "poly1d([ 1.,  0., -3.,  2.])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 = np.poly1d(a, r=True)\n",
    "p2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.237646Z",
     "end_time": "2024-05-07T21:11:14.289914Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   3\n",
      "1 x - 3 x + 2\n"
     ]
    }
   ],
   "source": [
    "print(p2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.254135Z",
     "end_time": "2024-05-07T21:11:14.289914Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "0.0"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2(1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.269977Z",
     "end_time": "2024-05-07T21:11:14.289914Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "0.0"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2(-2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.289914Z",
     "end_time": "2024-05-07T21:11:14.307569Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   2\n",
      "1 m - 2 m + 1\n"
     ]
    }
   ],
   "source": [
    "p3 = np.poly1d(a, variable=\"m\")\n",
    "print(p3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.311010Z",
     "end_time": "2024-05-07T21:11:14.373918Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p=    2\n",
      "1 x - 2 x + 1\n",
      "p2=    3\n",
      "1 x - 3 x + 2\n"
     ]
    }
   ],
   "source": [
    "print(\"p=\", p)\n",
    "print(\"p2=\", p2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.332373Z",
     "end_time": "2024-05-07T21:11:14.386137Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   3     2\n",
      "1 x + 1 x - 5 x + 3\n"
     ]
    }
   ],
   "source": [
    "print(p + p2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.352699Z",
     "end_time": "2024-05-07T21:11:14.386137Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    3     2\n",
      "-1 x + 1 x + 1 x - 1\n"
     ]
    }
   ],
   "source": [
    "print(p - p2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.379309Z",
     "end_time": "2024-05-07T21:11:14.446769Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   5     4     3     2\n",
      "1 x - 2 x - 2 x + 8 x - 7 x + 2\n"
     ]
    }
   ],
   "source": [
    "print(p * p2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.409677Z",
     "end_time": "2024-05-07T21:11:14.446769Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "(poly1d([1., 2.]), poly1d([0.]))"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 / p"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.430888Z",
     "end_time": "2024-05-07T21:11:14.446769Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   3\n",
      "1 x - 5 x + 3\n",
      "   5     4     3\n",
      "3 x + 2 x - 8 x + 1 x + 2\n"
     ]
    }
   ],
   "source": [
    "p4 = p2 + [-2, 1]\n",
    "p5 = p2 * np.array([3, 2, 1])\n",
    "print(p4)\n",
    "print(p5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.446769Z",
     "end_time": "2024-05-07T21:11:14.466903Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "(poly1d([ 1.5   , -1.25  , -5.125 , 10.1875]), poly1d([ -9.0625, -38.75  ]))"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p5 / [2, 3, 4]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.466903Z",
     "end_time": "2024-05-07T21:11:14.524268Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "poly1d([ 2, -2])"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.deriv()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.486309Z",
     "end_time": "2024-05-07T21:11:14.524766Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "poly1d([ 0.33333333, -1.        ,  1.        ,  0.        ])"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.integ()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.508065Z",
     "end_time": "2024-05-07T21:11:14.524766Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "poly1d([ 1., -2.,  1.])"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.integ().deriv()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.524766Z",
     "end_time": "2024-05-07T21:11:14.540493Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-2.49086362,  1.83424318,  0.65662043])"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.roots(p4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.545977Z",
     "end_time": "2024-05-07T21:11:14.606287Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1.,  0., -1.])"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = np.array([1, -1])\n",
    "fr = np.poly(r)\n",
    "fr"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.567299Z",
     "end_time": "2024-05-07T21:11:14.610887Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   2\n",
      "1 x - 1\n"
     ]
    }
   ],
   "source": [
    "print(np.poly1d(fr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.587697Z",
     "end_time": "2024-05-07T21:11:14.610887Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "x = np.linspace(0, 2 * np.pi, 10000)\n",
    "y = np.sin(x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.606512Z",
     "end_time": "2024-05-07T21:11:14.666496Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "f3 = np.polyfit(x, y, 3)\n",
    "f5 = np.polyfit(x, y, 5)\n",
    "f7 = np.polyfit(x, y, 7)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.631364Z",
     "end_time": "2024-05-07T21:11:14.667493Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [],
   "source": [
    "space3 = np.abs(np.polyval(f3, x) - y)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.659513Z",
     "end_time": "2024-05-07T21:11:14.686684Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-0.203109  , -0.2019105 , -0.20071271, ...,  0.20071271,\n        0.2019105 ,  0.203109  ])"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pf3 = np.poly1d(f3)\n",
    "pf3(x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.675208Z",
     "end_time": "2024-05-07T21:11:14.687677Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-0.203109  , -0.2019105 , -0.20071271, ...,  0.20071271,\n        0.2019105 ,  0.203109  ])"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.polyval(f3, x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.687677Z",
     "end_time": "2024-05-07T21:11:14.701655Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "space5 = np.abs(np.polyval(f5, x) - y)\n",
    "space7 = np.abs(np.polyval(f7, x) - y)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.701655Z",
     "end_time": "2024-05-07T21:11:14.717485Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "(0.20310899877775554, 0.015943766615306162, 0.0006625828771855954)"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(space3), np.max(space5), np.max(space7)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.717485Z",
     "end_time": "2024-05-07T21:11:14.756301Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.解线性方程组"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "array([2., 3.])"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[3, 1], [1, 2]])\n",
    "b = np.array([9, 8])\n",
    "x = np.linalg.solve(a, b)\n",
    "x"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.748538Z",
     "end_time": "2024-05-07T21:11:14.772021Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T21:11:14.772021Z",
     "end_time": "2024-05-07T21:11:14.812379Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
